package com.dtech.submp.controller;


import com.dtech.submp.common.BusiException;
import com.dtech.submp.model.UsersEntity;
import com.dtech.submp.service.UsersService;
import org.nutz.dao.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@CrossOrigin // 跨域请求
@RequestMapping("user")
public class UsersController {


	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UsersService userService;

	@RequestMapping(value = "getUsersById", method = RequestMethod.POST)
	public Object getUsersById(String user_id) {
		Map<String, Object> result = new HashMap<>();
		UsersEntity entity = userService.findById(user_id);

		result.put("code", "0");
		result.put("msg", "OK");
		result.put("data", entity);

		return result;
	}

	@RequestMapping(value = "fetchList", method = RequestMethod.POST)
	public Object fetchList(UsersEntity entity, int pageNumber, int pageSize, String order)
			throws BusiException {
		Map<String, Object> result = new HashMap<>();
		if (StringUtils.isEmpty(pageNumber)) {
			throw new BusiException("查询失败，页号不能为空！");
		}
		if (StringUtils.isEmpty(pageSize)) {
			throw new BusiException("查询失败，页容量不能为空！");
		}
		if (StringUtils.isEmpty(order)) {
			throw new BusiException("查询失败，排序方式不能为空！");
		}
			QueryResult qr = userService.findUserList(entity,pageNumber,pageSize);
			result.put("code", "0");
			result.put("msg", "OK");
			result.put("data", qr);

		return result;
	}

}